package com.cyan.teamquestionnaire.service.Impl;

import com.cyan.teamquestionnaire.beans.PageInfo;
import com.cyan.teamquestionnaire.entity.User;
import com.cyan.teamquestionnaire.mappers.UserMapper;
import com.cyan.teamquestionnaire.service.GroupService;
import com.cyan.teamquestionnaire.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    // 属性自动注入建议使用Resource而不是AutoWired
    @Resource
    private UserMapper userMapper;

    @Resource
    private GroupService groupService;

    // 通过姓名查询
    @Override
    public User selectByName(String name) {
        return userMapper.selectByName(name);
    }

    // 通过电话查询
    @Override
    public User selectByTel(String tel) {
        return userMapper.selectByTel(tel);
    }

    /**
     * @param u 前端直接传参数，service层检验结果
     * @return 0--创建成功，1--参数输入不合法，2--用户名已存在，3--手机号已存在
     */
    @Override
    public int createUser(User u) {
        String name = u.getName(), tel = u.getTel(), passwd = u.getPasswd();

        // 电话号码规格检查,最基本的检查就是不等于11位, 其他判空检查
        if (tel.length() != 11 || name.length() == 0 || passwd.length() == 0) return 1;

        User uN = selectByName(name);
        User uT = selectByTel(tel);

        if (uN != null) return 2;// 说明用户名已存在
        if (uT != null) return 3;// 手机号已存在

        userMapper.createUser(u);
        return 0;
    }

    // 批量查询--分页处理
    @Override
    public PageInfo<User> getManagedUsers(PageInfo<User> pageInfo) {
        List<User> list;
        // 判断前端是管理员还是租户，对应不同查询
        if (pageInfo.getLevel() == 2) {
            //租户查询自己管理的用户
            return tenantCreateUser(pageInfo);
        }
        if (pageInfo.getLevel() == 1) {
            //管理员返回所有租户信息
            list = userMapper.selectAllTenantAndUser("%" + pageInfo.getQuery() + "%");
            pageInfo.setTotal(list.size());
            List<User> res = new ArrayList<>();
            int pages = pageInfo.getPageNum();
            int pageSize = pageInfo.getPageSize();
            for (int i = (pages - 1) * pageSize, j = 0; i < list.size() && j < pageSize; i++, j++) res.add(list.get(i));
            pageInfo.setList(res);
        }
        return pageInfo;
    }

    @Override
    public int modifyUser(User user) {
        return userMapper.modifyUser(user);
    }

    @Override
   public int modifyUserTel(User u){return userMapper.modifyUserTel(u);}


    @Override
    public int deleteUser(User user) {
        userMapper.deleteUser(user);
        return 0;
    }
    //在租户和用户表中删除一条数据，删除用户的时候，相应的信息也删除
    @Override
   public int tenantDelteUser(String userName){
        userMapper.tenantDelteUser(userName);
        return 0;
    }

    //查询租户创建的用户
    @Override
    public PageInfo<User> tenantCreateUser(PageInfo<User> pageInfo) {
        List<String> namelist = userMapper.tenantCreateUser(pageInfo.getName(), "%" + pageInfo.getQuery() + "%");
        List<User> list = new ArrayList<>();
        for (String name : namelist) {
            User user = userMapper.selectByName(name);
            list.add(user);
        }
        return (PageInfo<User>) (PageInfo) groupService.splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list);
    }

    //租户创建用户加入表中
    @Override
    public int tenantAddUser(String tenantName, String userName) {
        return userMapper.tenantAddUser(tenantName, userName);
    }

    //返回所有的答者  level==4的成员
    @Override
    public PageInfo<User> selectAllAnswerer(PageInfo<User> pageInfo) {
        List<User> list = userMapper.selectAllAnswerer("%" + pageInfo.getQuery() + "%");
        return (PageInfo<User>) (PageInfo) groupService.splitPage((PageInfo<Object>) (PageInfo) pageInfo, (List<Object>) (List) list);
    }

    //通过答者列表进行用户增加
    @Override
    public int tenantCreateUserByAnsList(Map<String, Object> map) {

        String tenantName = (String) map.get("tenantName");
        String userName = (String) map.get("userName");
        Integer count = Integer.parseInt((String) map.get("count"));

        int i = userMapper.tenantCreateUserByAnsList(userName, count);
        int j = userMapper.tenantAddUser(tenantName, userName);
        if (i == j) {
            return 1;
        } else return 0;
    }

    @Override
    public List<String> tenantHasUser(String tenantName) {
        return userMapper.tenantCreateUser(tenantName, "%%");
    }
}
